// pages/playlist-cate/index.js
import {
  getPlaylistHotCate,
  getPlaylistAllCate,
  getPlaylistByCate
} from '../../services/playlist-api'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentTabName:'华语',
    hotCates:[],
    currentPlaylists:[],
    hasMore:true,
    before:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    getPlaylistHotCate().then(res => {
      const hotCates = res.tags
      this.setData({hotCates})
    })
    this.getPlaylistData(this.data.currentTabName)
  },
  onReachBottom: function () {
    if(!this.data.hasMore) return 
    this.getPlaylistData(this.data.currentTabName,this.data.before)
  },
  handleTabChange(e){
    const currentTabName = e.detail.name
    this.setData({currentTabName,currentPlaylists:[]})
    this.getPlaylistData(currentTabName)

  },
  getPlaylistData(currentTab,before = ''){
    getPlaylistByCate(currentTab,18,before).then(res => {
      const currentPlaylists = [...this.data.currentPlaylists,...res.playlists]
      const hasMore = res.more
      if(!res.playlists.length || res.playlists.length < 18) {
        this.setData({hasMore:false})
        return wx.showToast({
          title: ' 已经到底了~',
        })
      }

      const before = res.lasttime
      this.setData({currentPlaylists,hasMore,before})
    })
  }
})